/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.user;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.GwtRpcDataSource;
import net.bioteam.appweb.gwt.apps.clientandserver.ClusterInfo;
import net.bioteam.appweb.gwt.apps.clientandserver.user.ClusterRet;
import net.bioteam.appweb.gwt.apps.clientandserver.user.JobRet;
import net.bioteam.appweb.gwt.apps.clientandserver.user.UserAction;
import net.bioteam.appweb.submit.RunStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.SortSpecifier;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ClusterDataSource extends GwtRpcDataSource
{

	private static void copyValues(ClusterInfo ci, ListGridRecord to)
	{
		to.setAttribute("id", ci.clusterid);
		to.setAttribute("requestTime", ci.requestTime);
		to.setAttribute("status", ci.status.toString());
		to.setAttribute("hosts", ci.hostNum);
		to.setAttribute("note", ci.note);
		to.setAttribute("elapsed", ci.elapsedHour);
	}

	public ClusterDataSource()
	{
		super();
		DataSourceTextField idField = new DataSourceTextField("id");
		idField.setPrimaryKey(true);

		DataSourceDateTimeField requestTimeField = new DataSourceDateTimeField("requestTime");;

		DataSourceIntegerField hostsField = new DataSourceIntegerField("hosts");
		DataSourceTextField statusField = new DataSourceTextField("status");
		DataSourceTextField noteField = new DataSourceTextField("note");
		DataSourceIntegerField elapsedField = new DataSourceIntegerField("elapsed");
		

		setFields(idField, requestTimeField, statusField, hostsField, noteField, elapsedField);
	}

	@Override
	protected void executeAdd(String requestId, DSRequest request, DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);
	}

	@Override
	protected void executeFetch(final String requestId, final DSRequest request, final DSResponse response)
	{
		GWT.log("start "+request.getStartRow()+" end "+request.getEndRow(), null);
		ApplicationCallBack<ClusterRet> callback = new ApplicationCallBack<ClusterRet>()
		{

			public void onFailureAfter(Throwable caught)
			{
				
				response.setStatus(RPCResponse.STATUS_FAILURE);
				processResponse(requestId, response);
				SC.warn(caught.getMessage(), StaticHolder.dumpB);
			}

			public void onSuccessAfter(ClusterRet result)
			{

				ListGridRecord[] list = new ListGridRecord[result.cis.length];
				for (int i = 0; i < list.length; i++)
				{
					ListGridRecord record = new ListGridRecord();
					copyValues(result.cis[i], record);
					list[i] = record;

				}
				
				response.setData(list);
				// IMPORTANT: for paging to work we have to specify size of full result set
				//response.setStartRow(request.getStartRow());
				//response.setEndRow(request.getStartRow()+list.length);
                response.setTotalRows (result.count);
				processResponse(requestId, response);
			}

			public void execute()
			{
				
				UserAction.Util.getInstance().getClusterHistory(request.getStartRow(), request.getEndRow() - request.getStartRow(),this);
			}

		};
		callback.execute();

	}


	protected void executeRemove(final String requestId, final DSRequest request, final DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);

	}

	@Override
	protected void executeUpdate(String requestId, DSRequest request, DSResponse response)
	{
		//this is not supported
		response.setStatus(RPCResponse.STATUS_FAILURE);
		processResponse(requestId, response);

	}

}
